﻿using Caliburn.Micro;
using SmallTune.Messages;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SmallTune.ViewModels
{
    [Export(typeof(PlaylistViewModel))]
    public class PlaylistViewModel : Screen
    {
        private IEventAggregator eventAggregator;
        
        public PlaylistViewModel(IEventAggregator eventAggregator)
        {
            // TODO: Complete member initialization
            this.eventAggregator = eventAggregator;
            this.eventAggregator.Subscribe(this);
        }

        //Adds a song
        public void AddSong()
        {
            string songPath = "";
                //Display Dialog
                Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
                dlg.FileName = ""; // Default file name 
                dlg.DefaultExt = ".mp3"; // Default file extension 
                dlg.Filter = "Audio files |*.mp3;*.flac;*.wav"; // Filter files by extension 

                // Show open file dialog box 
                Nullable<bool> result = dlg.ShowDialog();

                // Process open file dialog box results 
                if (result == true)
                {
                    // Open document 
                    songPath = dlg.FileName;
                    Task AddSong = Task.Factory.StartNew(() => AddSongToDB(songPath));
                }
        }

        private void AddSongToDB(string songPath)
        {
            eventAggregator.Publish(new PlaySongMessage { path = songPath });
        }
    }
}
